<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" media="screen" href="../../css/bootstrap.min.css">
    <script type="text/javascript" src="../../js/libs/jquery-2.1.1.min.js"></script>
    <link rel="stylesheet" type="text/css" href="../../css/bootstrap-select.min.css">
    <script src="../../js/bootstrap/bootstrap.min.js"></script>
    <script src="../../js/bootstrap/bootstrap-select.min.js"></script>
</head>
<body>
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
    <form class="form-horizontal" onsubmit="return false" id="form">
        <fieldset>
            <div class="form-group">
                <label class="col-md-1 control-label">表名</label>
                <div class="col-md-3">
                    <select class="form-control input-sm selectpicker show-tick" name="tableText" id="tableText"
                            data-live-search="true"></select>
                </div>
                <div class="col-md-2">
                    <button id="showInfo" class="btn btn-primary">预览代码</button>
                </div>
                <label class="col-md-1 control-label">类型</label>
                <div class="col-md-3">
                    <select class="form-control input-sm selectpicker show-tick" name="category" id="category"
                            data-live-search="true">
                        <option value="0" selected>普通单表</option>
                        <option value="1">树结构表</option>
                    </select>
                </div>
                <div class="col-md-2">
                    <button id="save" class="btn btn-primary">保存代码</button>
                </div>
            </div>
        </fieldset>
        <div id="detail">
	        <fieldset>
	           <table class="table">
                  <caption>基本配置</caption>
                  <tbody>
                    <tr>
                      <td width="7%">路径</td>
                      <td width="20%">
                          <input class="form-control" placeholder="路径" type="text" name="path" id="path" value="d:/generate"
                                data-bv-notempty="true"
                                data-bv-notempty-message="路径 不能为空">
                      </td>
                      <td width="7%">功能描述</td>
                      <td width="15%">
                          <input type="hidden" name="tableName" id="tableName" value="">
                          <input class="form-control" placeholder="功能描述" type="text" name="tableComment" id="tableComment" value="">
                      </td>
                      <td width="7%">模块名</td>
                      <td width="20%">
                          <input class="form-control" placeholder="模块名" type="text" name="moduleName" id="moduleName" value=""
                                 data-bv-notempty="true"
                                 data-bv-notempty-message="模块名 不能为空">
                      </td>
                      <td width="7%"></td>
                      <td width="15%"></td>
                    </tr>
                    <tr>
                      <td>bean包名</td>
                      <td>
                        <input class="form-control" placeholder="bean包名" type="text" name="beanPackageName" id="beanPackageName" value="com.lty.jeeg.server.modules.model"
                                data-bv-notempty="true"
                                data-bv-notempty-message="bean包名 不能为空">
                      </td>
                      <td>bean类名</td>
                      <td>
                        <input class="form-control" placeholder="bean类名" type="text" name="beanName" id="beanName" value="BeanTest"
                                data-bv-notempty="true"
                                data-bv-notempty-message="bean类名 不能为空">
                      </td>
                      <td>dao包名</td>
                      <td>
                        <input class="form-control" placeholder="dao包名" type="text" name="daoPackageName" id="daoPackageName" value="com.lty.jeeg.server.modules.dao"
                                data-bv-notempty="true"
                                data-bv-notempty-message="dao包名 不能为空">
                      </td>
                      <td>dao类名</td>
                      <td>
                        <input class="form-control" placeholder="dao类名" type="text" name="daoName" id="daoName" value="BeanTestDao"
                                data-bv-notempty="true"
                                data-bv-notempty-message="dao类名 不能为空">
                      </td>
                    </tr>
                    <tr>
                        <td>service包名</td>
                        <td>
                            <input class="form-control" placeholder="service包名" type="text" name="servicePkgName" id="servicePkgName" value="com.lty.jeeg.server.modules.service"
                                   data-bv-notempty="true"
                                   data-bv-notempty-message="service包名 不能为空">
                        </td>
                        <td>service类名</td>
                        <td>
                            <input class="form-control" placeholder="service类名" type="text" name="serviceName" id="serviceName" value="Service"
                                   data-bv-notempty="true"
                                   data-bv-notempty-message="service类名 不能为空">
                        </td>
                        <td>serviceImpl包名</td>
                        <td>
                            <input class="form-control" placeholder="serviceImpl包名" type="text" name="serviceImplPkgName" id="serviceImplPkgName" value="com.lty.jeeg.server.modules.service.impl"
                                   data-bv-notempty="true"
                                   data-bv-notempty-message="serviceImpl包名 不能为空">
                        </td>
                        <td>serviceImpl类名</td>
                        <td>
                            <input class="form-control" placeholder="serviceImpl类名" type="text" name="serviceImplName" id="serviceImplName" value="ServiceImpl"
                                   data-bv-notempty="true"
                                   data-bv-notempty-message="serviceImpl类名 不能为空">
                        </td>
                    </tr>
                    <tr>
                        <td>controller包名</td>
	                    <td>
	                        <input class="form-control" placeholder="controller包名" type="text" name="controllerPkgName" id="controllerPkgName" value="com.lty.jeeg.server.modules.controller"
	                                data-bv-notempty="true"
	                                data-bv-notempty-message="controller包名 不能为空">
	                    </td>
                        <td>controller类名</td>
                        <td>
                         <input class="form-control" placeholder="controller类名" type="text" name="controllerName" id="controllerName" value="ControllerTest"
                                data-bv-notempty="true"
                                data-bv-notempty-message="controller类名 不能为空">
                        </td>
                    </tr>
                  </tbody>
                </table>
	        </fieldset>
            <table class="table">
                <caption>字段详情</caption>
                <thead>
                    <tr>
                        <th>表字段名</th>
                        <th>表类型</th>
                        <th>字段描述<label style="color: red">✲</label></th>
                        <th>java字段名<label style="color: red">✲</label></th>
                        <th>java字段类型<label style="color: red">✲</label></th>
                        <th>默认值</th>
                    </tr>
                </thead>
                <tbody id="beanFields">
                </tbody>
            </table>
        </div>
    </form>
</div>

    <script type="text/javascript" src="../../js/jq.js"></script>
    <script type="text/javascript" src="../../layui/layui.js"></script>
	<script type="text/javascript" src="../../js/plugin/bootstrapvalidator/bootstrapValidator.min.js"></script>
    <script type="text/javascript" src="../../js/common.js"></script>
    <script type="text/javascript">
    layui.use('layer', function(){
	    var layer = layui.layer;
	});
    
    $(function () {
        var packageName = 'com.lty.jeeg.server.modules.';
        $('#moduleName, #subModuleName').bind('input propertychange', function () {
            var moduleName = $('#moduleName').val();
            $("#beanPackageName").val(packageName + moduleName + ".model");
            $("#daoPackageName").val(packageName + moduleName + ".dao");
            $("#servicePkgName").val(packageName + moduleName + ".service");
            $("#serviceImplPkgName").val(packageName + moduleName + ".service.impl");
            $("#controllerPkgName").val(packageName + moduleName + ".controller");
        });

        $('#beanName').bind('input propertychange', function () {
            var beanName = $('#beanName').val();
            $("#daoName").val(beanName + "Dao");
            $("#serviceName").val(beanName + "Service");
            $("#serviceImplName").val(beanName + "ServiceImpl");
            $("#controllerName").val(beanName + "Controller");
        })
    });

    initTableNameSelect();

    // 初始化表名下拉框
    function initTableNameSelect() {
        $.ajax({
            type : 'get',
            url : '/generate/tables',
            async: false,
            success : function(json) {
                if (json.code === 0) {
                    var data = json.data;
                    $.each(data, function (i, val) {
                        var text = val.tableName + ':' + val.tableComment;
                        $('#tableText').append("<option value ='" + text + "'>" + text + "</option>");
                    })
                }
            }
        })
    }
    
    $('#form').bootstrapValidator();
    $("#detail").hide();
    $("#save").hide();

    // 下划线转换驼峰
    function toHump(name) {
        return name.replace(/\_(\w)/g, function(all, letter){
            return letter.toUpperCase();
        });
    }
    // 驼峰转换下划线
    function toLine(name) {
        return name.replace(/([A-Z])/g,"_$1").toLowerCase();
    }

    function upperStart(s) {
        return s.slice(0, 1).toUpperCase() + s.slice(1);
    }
    
    $("#showInfo").click(function(){
        var bootstrapValidator = $("#form").data('bootstrapValidator');
        bootstrapValidator.validate();
        if(!bootstrapValidator.isValid()){
           return;
        }
        var beanFields = $("#beanFields");
        beanFields.html("");
        $.ajax({
            type : 'get',
            url : '/generate',
            data : "tableName=" + $("#tableText").val().split(':')[0],
            success : function(data) {
                $("#detail").show();
                $("#save").show();

                $('#tableName').val($("#tableText").val().split(':')[0]);
                $('#tableComment').val($("#tableText").val().split(':')[1]);
                
                var beanName = data.beanName;
                var arr = toLine(beanName).split("_").map(upperStart);
                var module = arr[1];
                beanName = beanName.slice(module.length);

                $("#moduleName").val(module.toLowerCase());

                var moduleName = $('#moduleName').val();

                var packageName = 'com.lty.jeeg.server.modules.';
                $("#beanPackageName").val(packageName + moduleName + ".model");
                $("#daoPackageName").val(packageName + moduleName + ".dao");
                $("#servicePkgName").val(packageName + moduleName + ".service");
                $("#serviceImplPkgName").val(packageName + moduleName + ".service.impl");
                $("#controllerPkgName").val(packageName + moduleName + ".controller");

                $("#beanName").val(beanName);
                $("#daoName").val(beanName + "Dao");
                $("#serviceName").val(beanName + "Service");
                $("#serviceImplName").val(beanName + "ServiceImpl");
                $("#controllerName").val(beanName + "Controller");
                var fields = data.fields;
                var length = fields.length;
                
                for(var i=0;i<length;i++){
                    var f = fields[i];
                    var tr = $("<tr></tr>");
                    
                    var columnName = f['columnName'];
                    var columnNameInput = "<input type='hidden' name='columnNames' value='" + columnName +"'>";
                    
                    tr.append("<td>" + columnNameInput + columnName + "</td>");
                    tr.append("<td>" + f['columnType'] + "</td>");

                    var columnComment = f['columnComment'];
                    var columnCommentInput = "<input name='beanFieldComment' class='form-control' value='" + columnComment +"'>";
                    tr.append("<td>" + columnCommentInput + "</td>");

                    var name = f['name'];
                    var nameInput = "<input name='beanFieldName' class='form-control' value='" + name +"'>";
                    tr.append("<td>" + nameInput + "</td>");
                    
                    var type = f['type'];
                    var typeInput = "<input name='beanFieldType' class='form-control' value='" + type +"'>";
                    tr.append("<td>" + typeInput + "</td>");
                    
                    var val = f['columnDefault'];
                    var valInput = "<input name='beanFieldValue' class='form-control' value='" + val +"'>";
                    tr.append("<td>" + valInput + "</td>");
                    
                    beanFields.append(tr);
                }
            }
        });
    });
    
    $("#save").click(function(){
        var bootstrapValidator = $("#form").data('bootstrapValidator');
        bootstrapValidator.validate();
        if(!bootstrapValidator.isValid()){
           return;
        }
        if($("#detail").is(':hidden')) {
           return;
        }


        var formdata = $("#form").serializeObject();
        $.ajax({
            type : 'post',
            url : '/generate',
            contentType: "application/json; charset=utf-8",  
            data : JSON.stringify(formdata),
            success : function(data) {
                $("#detail").hide();
                $("#save").hide();
                layer.msg("生成成功");
            }
        });
    });

    
    </script>
</body>
</html>